﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 粒子裁剪脚本 use UIWidget
/// 
/// binding this script to particle system
/// reference a uiwidget which is it's parent
/// then clip particle system in the arena
///  ---
/// |   |
///  ___
/// </summary>
//[ExecuteInEditMode]
//[RequireComponent(typeof(ParticleSystemRenderer))]
public class UIParticleClip : MonoBehaviour 
{
  

    [Header("必须指定裁剪区域 ")]
    public UIWidget parent;

    [Header("必须指定在cover作为层级显示")]
    public UIWidget cover;

    private ParticleSystemRenderer pRenderer;
    private Material dyMaterial;
    public Camera target;

    void OnEnable()
    {
        pRenderer = GetComponent<ParticleSystemRenderer>();
        dyMaterial = pRenderer.material;
        dyMaterial.EnableKeyword("CLIP_ON");
        target = NGUITools.FindCameraForLayer(gameObject.layer);
    }

    void OnDisable()
    {
        dyMaterial.DisableKeyword("CLIP_ON");
    }


    void OnWillRenderObject()
    {
        if (cover != null && cover.isActiveAndEnabled && cover.drawCall != null)
        {
            dyMaterial.renderQueue = cover.drawCall.renderQueue + 1;
        }

        if (parent != null && parent.isActiveAndEnabled)
        {
            Vector2 localCenter = Vector2.zero;
            Vector2 localSize = Vector2.zero;
            if (pRenderer.renderMode == ParticleSystemRenderMode.Mesh) 
            {
                Vector3 cameraPos = parent.transform.position - transform.position;
                UnityEngine.Debug.Log(" cameraPos  " + cameraPos + " parent.localCenter " + parent.localCenter);
                float scale = parent.transform.lossyScale.x;
                localCenter = cameraPos + parent.localCenter * scale;
                UnityEngine.Debug.Log(" localCenter  " + localCenter);
                localSize = parent.localSize * 0.5f * scale;
            }
            else {

                Vector3 cameraPos = target.worldToCameraMatrix.MultiplyPoint(parent.transform.position);
                float scale = parent.transform.lossyScale.x;
                localCenter = parent.localCenter * scale + cameraPos;
                localSize = parent.localSize * 0.5f * scale;
                //new Vector2(parent.localSize.x * 0.5f, parent.localSize.y * 0.5f);

                //UnityEngine.Debug.Log(" localCenter " + parent.localCenter.ToString());
                //UnityEngine.Debug.Log(" parent.transform.localPosition " + parent.transform.localPosition.ToString());
                //UnityEngine.Debug.Log(" localSize " + parent.localSize.ToString());


                //float scale = 1.0f / transform.lossyScale.x;
                //float scale = 1.0f;
            }

            Vector4 clipRange =
                new Vector4(-localCenter.x / localSize.x, -localCenter.y / localSize.y,
                            1f / localSize.x, 1f / localSize.y);
            dyMaterial.SetVector(Shader.PropertyToID("_ClipRange0"), clipRange);
        }
    }
}
